class Solution:
    def compress(self, chars: List[str]) -> int:
        if len(chars) == 1:
            return len(chars)
        sign = chars[0]
        s = ""
        num = 1

        for i in range(1,len(chars)):
            # 字母不同前面字符要压缩
            if chars[i] != sign:
                s += sign
                if num > 1:
                    s += str(num)
                    num = 1
                sign = chars[i]
            else:
                num += 1
            # 处理结束的情况
            if i == len(chars) - 1:
                s += sign
                if num > 1:
                    s += str(num)

        # 修改原字符串
        for i in range(len(s)):
            if chars[i] != s[i]:
                chars[i] = s[i]
        # 删除多余位置空间
        del chars[len(s):]
        return len(chars)